﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace WeChatParking.Util
{
    public class Cache
    {
        private static System.Web.Caching.Cache CacheProvider
        {
            get
            {
                return HttpRuntime.Cache;
            }
        }

        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        public static object GetCache(string CacheKey)
        {
            return CacheProvider.Get(CacheKey);
        }

        public static T GetCache<T>(string CacheKey)
        {
            var obj = GetCache(CacheKey);
            if (obj == null)
                return default(T);
            return (T)obj;
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public static void SetCache(string CacheKey, object CacheData)
        {
            CacheProvider.Insert(CacheKey, CacheData);
        }

        /// <summary>
        /// 设置数据缓存(相对时间滑动缓存)
        /// </summary>
        public static void SetCache(string CacheKey, object CacheData, TimeSpan Timeout)
        {
            CacheProvider.Insert(CacheKey, CacheData, null, System.Web.Caching.Cache.NoAbsoluteExpiration, Timeout);
        }

        /// <summary>
        /// 设置数据缓存（绝对过期时间）
        /// </summary>
        public static void SetCache(string CacheKey, object CacheData, DateTime absoluteExpiration)
        {
            CacheProvider.Insert(CacheKey, CacheData, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public static void SetCache(string CacheKey, object CacheData, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            CacheProvider.Insert(CacheKey, CacheData, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        public static void RemoveCache(string CacheKey)
        {
            CacheProvider.Remove(CacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            var _cache = CacheProvider;
            System.Collections.IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                _cache.Remove(CacheEnum.Key.ToString());
            }
        }

        public static Dictionary<string, object> GetAllCache()
        {
            var allData = new Dictionary<string, object>();
            var _cache = CacheProvider;
            System.Collections.IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                var key = CacheEnum.Key.ToString();
                if (key.IndexOf("__AppStartPage__~/") == -1)
                {
                    allData[key] = CacheEnum.Value;
                }
            }
            return allData;
        }
    }
}
